package com.whfc.fse.pushPlatform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whfc.base.service.SyncConfigService;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.HttpUtil;
import com.whfc.common.util.MD5Util;
import com.whfc.fse.entity.FseSync;
import com.whfc.fse.pushPlatform.model.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class ShiShouFseApi {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    // 塔吊基础数据上传
    public final static String reportCraneBase = "/api/v1/reportDeviceCraneBase/deviceCraneBase";
    // 塔吊实时数据上传
    public final static String reportRealCrane = "/api/v1/reportCrane/crane";
    // 塔吊报警数据上传
    public final static String reportCraneViolation = "/api/v1/reportCrane/violation";
    // 升降机基础数据上传
    public final static String reportLiftBase = "/api/v1/reportDeviceLiftBase/deviceLiftBase";
    // 升降机实时数据上传
    public final static String reportRealLift = "/api/v1/reportDeviceLift/deviceLift";
    // 升降机报警数据上传
    public final static String reportLiftViolation = "/api/v1/reportDeviceLift/violation";

    public final static String deviceUser = "/api/v1/reportDeviceUser/deviceUser";

    @DubboReference(interfaceClass = SyncConfigService.class, version = "1.0.0")
    private SyncConfigService syncConfigService;



    /**
     * 获取token
     * @return
     */
    public String getToken(String key,String secret,String url) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", key);
        jsonObject.put("password", secret);
        String getToken = "/api/v1/uploadApi/getToken";
        String result = HttpUtil.doPost(url + getToken, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("调用结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), jsonData.getString("msg"));
        }
        JSONObject data = jsonData.getJSONObject("data");
        return data.getString("token");
    }

    @Test
    public void testGetToken() {
        String token = getToken("xfyh", "xfyh123456", "http://122.189.98.132:11009/smart-cloud");

    }

    /**
     * 获取签名
     * @param data
     * @param fseSync
     * @return
     */
    public String getSign(Object data, FseSync fseSync) {
        String token = syncConfigService.getShiShouSyncToken(fseSync.getAppKey(),fseSync.getAppSecret(),fseSync.getHost());
        String json = JSON.toJSONString(data);
        String unSignStr = fseSync.getValue1() + json + token;
        String newSign = MD5Util.md5Encode(unSignStr);
        return newSign;
    }


    @Test
    public void testReportCraneBase() {
        FseSync fseSync = new FseSync();
        fseSync.setAppKey("xfyh");
        fseSync.setAppSecret("xfyh123456");
        fseSync.setHost("http://122.189.98.132:11009/smart-cloud");
        String cityId = "421802";
        fseSync.setValue1(cityId);
        ShiShouCraneBaseDTO baseDTO = new ShiShouCraneBaseDTO();
        baseDTO.setRecDate("2022-01-22 13:33:12");
        baseDTO.setDeviceId("16394");
        baseDTO.setCreanceId("16394");
        baseDTO.setShortArm(12.0);
        baseDTO.setLongArm(60.0);
        baseDTO.setCraneHeight(62.0);
        baseDTO.setMaxCraneLoad(5.0);
        baseDTO.setMaxRatedKn(5000.0);
        baseDTO.setMaxAngle(360.0);
        baseDTO.setMaxRadius(50.0);
        baseDTO.setMaxHeight(60.0);
        baseDTO.setMaxWindSpeed(5.0);
        reportCraneBase(baseDTO, fseSync);
    }

    /**
     * 石首塔吊基础数据上传
     */
    public void reportCraneBase(ShiShouCraneBaseDTO shiShouCraneBaseDTO, FseSync fseSync) {
        logger.info("石首塔吊基础数据上传：{}", shiShouCraneBaseDTO.toString());
        JSONObject jsonObject = new JSONObject();
        String token = getToken("xfyh", "xfyh123456", "http://122.189.98.132:11009/smart-cloud");
        jsonObject.put("token", token);

        String json = JSON.toJSONString(shiShouCraneBaseDTO);
        String sign = MD5Util.md5Encode(fseSync.getValue1() + json + token);
        jsonObject.put("sign", sign);
        jsonObject.put("body", Arrays.asList(shiShouCraneBaseDTO));
        String result = HttpUtil.doPost(fseSync.getHost() + reportCraneBase, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("石首塔吊基础数据上传结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), "石首塔吊基础数据上传失败");
        }
    }

    /**
     * 石首塔吊实时数据上传
     */
    public void reportRealCrane(ShiShouCraneRealDTO shouCraneRealDTO, FseSync fseSync) {
        logger.info("石首塔吊实时数据上传：{}", shouCraneRealDTO.toString());
        JSONObject jsonObject = new JSONObject();
        String token = syncConfigService.getShiShouSyncToken(fseSync.getAppKey(),fseSync.getAppSecret(),fseSync.getHost());
        jsonObject.put("token", token);
        jsonObject.put("sign", getSign(shouCraneRealDTO, fseSync));
        jsonObject.put("body", Arrays.asList(shouCraneRealDTO));
        String result = HttpUtil.doPost(fseSync.getHost() + reportRealCrane, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("石首塔吊实时数据上传结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), "石首塔吊实时数据上传失败");
        }
    }

    /**
     * 石首塔吊报警数据上传
     */
    public void reportCraneViolation(ShiShouCraneWarnDTO shiShouCraneWarnDTO, FseSync fseSync) {
        logger.info("石首塔吊报警数据上传：{}", shiShouCraneWarnDTO.toString());
        JSONObject jsonObject = new JSONObject();
        String token = syncConfigService.getShiShouSyncToken(fseSync.getAppKey(),fseSync.getAppSecret(),fseSync.getHost());
        jsonObject.put("token", token);
        jsonObject.put("sign", getSign(shiShouCraneWarnDTO, fseSync));
        jsonObject.put("body", Arrays.asList(shiShouCraneWarnDTO));
        String result = HttpUtil.doPost(fseSync.getHost() + reportCraneViolation, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("石首塔吊报警数据上传结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), "石首塔吊报警数据上传失败");
        }
    }

    @Test
    public void testreportLiftBase() {
        FseSync fseSync = new FseSync();
        fseSync.setAppKey("xfyh");
        fseSync.setAppSecret("xfyh123456");
        fseSync.setHost("http://122.189.98.132:11009/smart-cloud");
        String cityId = "421802";
        fseSync.setValue1(cityId);
        ShiShouLiftBaseDTO baseDTO = new ShiShouLiftBaseDTO();
        baseDTO.setRecDate("2022-01-22 13:33:12");
        baseDTO.setDeviceId("1111");
        baseDTO.setCreanceId("tuanyue-1111");
        baseDTO.setMaxLoad(5000.0);
        baseDTO.setMaxHeight(100.0);
        baseDTO.setMaxSpeed(3.0);
        baseDTO.setMaxWindSpeed(5.0);
        baseDTO.setMaxAngle(360.0);
        baseDTO.setMaxPeopleNum(10);
        reportLiftBase(baseDTO, fseSync);
    }

    /**
     * 石首升降机基础数据上传
     */
    public void reportLiftBase(ShiShouLiftBaseDTO shiShouLiftBaseDTO, FseSync fseSync) {
        logger.info("石首升降机基础数据上传：{}", shiShouLiftBaseDTO.toString());
        JSONObject jsonObject = new JSONObject();
        String token = getToken("xfyh", "xfyh123456", "http://122.189.98.132:11009/smart-cloud");
        jsonObject.put("token", token);
        String json = JSON.toJSONString(shiShouLiftBaseDTO);
        String sign = MD5Util.md5Encode(fseSync.getValue1() + json + token);
        jsonObject.put("sign", sign);
        jsonObject.put("body", Arrays.asList(shiShouLiftBaseDTO));
        String result = HttpUtil.doPost(fseSync.getHost() + reportLiftBase, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("石首升降机基础数据上传结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), "石首升降机基础数据上传失败");
        }
    }

    /**
     * 石首升降机实时数据上传
     */
    public void reportRealLift(ShiShouLiftRealDTO shiShouLiftRealDTO, FseSync fseSync) {
        logger.info("石首升降机实时数据上传：{}", shiShouLiftRealDTO.toString());
        JSONObject jsonObject = new JSONObject();
        String token = syncConfigService.getShiShouSyncToken(fseSync.getAppKey(),fseSync.getAppSecret(),fseSync.getHost());
        jsonObject.put("token", token);
        jsonObject.put("sign", getSign(shiShouLiftRealDTO, fseSync));
        jsonObject.put("body", Arrays.asList(shiShouLiftRealDTO));
        String result = HttpUtil.doPost(fseSync.getHost() + reportRealLift, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("石首升降机实时数据上传结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), "石首升降机实时数据上传失败");
        }
    }

    /**
     * 石首升降机报警数据上传
     */
    public void reportLiftViolation(ShiShouCraneWarnDTO shiShouCraneWarnDTO, FseSync fseSync) {
        logger.info("石首升降机报警数据上传：{}", shiShouCraneWarnDTO.toString());
        JSONObject jsonObject = new JSONObject();
        String token = syncConfigService.getShiShouSyncToken(fseSync.getAppKey(),fseSync.getAppSecret(),fseSync.getHost());
        jsonObject.put("token", token);
        jsonObject.put("sign", getSign(shiShouCraneWarnDTO, fseSync));
        jsonObject.put("body", Arrays.asList(shiShouCraneWarnDTO));
        String result = HttpUtil.doPost(fseSync.getHost() + reportLiftViolation, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("石首升降机报警数据上传结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), "石首升降机报警数据上传失败");
        }
    }

    /**
     * 石首升降机基础数据上传
     */
    public void reportDeviceUser(ShiShouDeviceUserDTO shiShouDeviceUserDTO, FseSync fseSync) {
        logger.info("石首升降机或塔吊操作人员数据上传：{}", shiShouDeviceUserDTO.toString());
        JSONObject jsonObject = new JSONObject();
        String token = getToken("xfyh", "xfyh123456", "http://122.189.98.132:11009/smart-cloud");
        jsonObject.put("token", token);
        String json = JSON.toJSONString(shiShouDeviceUserDTO);
        String sign = MD5Util.md5Encode(fseSync.getValue1() + json + token);
        jsonObject.put("sign", sign);
        jsonObject.put("body", Arrays.asList(shiShouDeviceUserDTO));
        String result = HttpUtil.doPost(fseSync.getHost() + deviceUser, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("石首升降机或塔吊操作人员数据上传结果：{}", jsonData);
        if (200 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), "石首升降机或塔吊操作人员数据上传失败");
        }
    }

    @Test
    public void testreportDeviceUser() {
        FseSync fseSync = new FseSync();
        fseSync.setAppKey("xfyh");
        fseSync.setAppSecret("xfyh123456");
        fseSync.setHost("http://122.189.98.132:11009/smart-cloud");
        String cityId = "421802";
        fseSync.setValue1(cityId);
        ShiShouDeviceUserDTO shiShouDeviceUserDTO = new ShiShouDeviceUserDTO();
        shiShouDeviceUserDTO.setRecDate("2024-03-15 13:33:12");
        shiShouDeviceUserDTO.setDeviceId("24020504");
        shiShouDeviceUserDTO.setCreanceId("24020504");
        shiShouDeviceUserDTO.setInOrOut("1");
        shiShouDeviceUserDTO.setUserName("李军");
        shiShouDeviceUserDTO.setType("1");

        reportDeviceUser(shiShouDeviceUserDTO, fseSync);
    }

}
